package com.example.springbootshiro.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ShrioController {
    @RequestMapping({"/","/index"})
    public String doindex(Model model){
        model.addAttribute("msg","hi shrio");
        return "index";
    }

    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(String name,String pass,Model model){
        //获取当前登录对象 shrio安全登录
        Subject subject = SecurityUtils.getSubject();

        //封装用户登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(name,pass);
        try {
            subject.login(token); //执行登录方法 没有异常说明ok
            return "index";
        } catch (Exception e) {
            model.addAttribute("msg","登录失败");
                return "login";
        }
    }
    @RequestMapping("/user/add")
    public String add(){
        return "user/add";
    }

    @RequestMapping("/user/update")
    public  String update(){
        return "user/update";
    }
}
